<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //promise.all
      //所有promise返回成功则成功 失败则失败 失败信息为失败的promise信息
      Promise.myAll = function (list) {
        //返回一个promise对象
        return new Promise((resolve, reject) => {
          let cont = 0;
          let result = [];
          list.forEach((promise) => {
            promise.then(
              (res) => {
                cont++;
                result.push(res);
                //全都为成功才返回成功
                if (cont === list.length) {
                  resolve(result);
                }
              },
              (reson) => {
                //如果出现失败则返回失败
                reject(reson);
              }
            );
          });
        });
      };

      let p1 = Promise.resolve(1);
      let p2 = Promise.resolve(2);
      let p3 = Promise.resolve(3);

      let promiseArr = [p1, p2, p3];
      Promise.myAll(promiseArr).then(
        (res) => {
          console.log(res);
        },
        (reson) => {
          console.log(reson);
        }
      );
    </script>
  </body>
</html>
